angular.module('theApp.controllers', [])

.controller('errorController', function($scope, $rootScope, $routeParams) {
   $scope.msg = $rootScope.errors[$routeParams.id];
 })
    
.controller('requestController', function($scope, $rootScope, $firebase, $filter, $location, $route, $routeParams, fbURL, ssw_service) {
    //ssw_service.preamble($rootScope, $firebase, fbURL);
    //ssw_service.update_auth($rootScope, $firebase, $firebaseSimpleLogin, fbURL, ssw_service);
    $scope.requestFocus = true;
    ssw_service.controller_one($scope, $rootScope, $firebase, $location, $route, fbURL, ssw_service, 'request', function(ids) {
        ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'field', ids);
        ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'team', ids, function(ids) {
            ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'user', ids);            
        });
     }, $routeParams.id);
})

.controller('teamController', function($scope, $rootScope, $firebase, $filter, $location, $route, $routeParams, fbURL, ssw_service) {
    $scope.teamFocus = true;
    ssw_service.controller_one($scope, $rootScope, $firebase, $location, $route, fbURL, ssw_service, 'team', function(ids) {
        ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'user', ids);
        ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'request', ids);
    }, $routeParams.id);    
 })
    
.controller('userController', function($scope, $rootScope, $firebase, $filter, $location, $route, $routeParams, fbURL, ssw_service) {
    //ssw_service.preamble($rootScope, $firebase, fbURL);
    //ssw_service.update_auth($rootScope, $firebase, $firebaseSimpleLogin, fbURL, ssw_service);
    $scope.userFocus = true;
    ssw_service.controller_one($scope, $rootScope, $firebase, $location, $route, fbURL, ssw_service, 'user', function(ids) {
        ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'team', ids);
    }, $routeParams.id);
 })

.controller('fieldController', function($scope, $rootScope, $firebase, $filter, $location, $route, $routeParams, fbURL, ssw_service) {
    //ssw_service.preamble($rootScope, $firebase, fbURL);
    //ssw_service.update_auth($rootScope, $firebase, $firebaseSimpleLogin, fbURL, ssw_service);
    $scope.fieldFocus = true;
    ssw_service.controller_one($scope, $rootScope, $firebase, $location, $route, fbURL, ssw_service, 'field', function(ids) {
        ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'request', ids, function(ids) {
            ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'team', ids, function(ids) {
                ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'user', ids);
            });
        }, true);
    }, $routeParams.id);
 })
 
 .controller('fieldsController', function($scope, $rootScope, $firebase, $filter, $location, $route, $routeParams, fbURL, ssw_service) {
    ssw_service.preamble($rootScope, $firebase, fbURL);
})

.controller('allController', function($scope, $rootScope, $firebase, $filter, $location, $route, $routeParams, fbURL, ssw_service) {
    ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'user',    true);
    ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'team',    true);
    ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'request', true);
    ssw_service.controller_some($scope, $firebase, $filter, fbURL, ssw_service, 'field',   true);
   
})
    
.controller('teamControllerCreate', function($scope, $rootScope, $firebase, $location, $route, $routeParams, fbURL, ssw_service) {
    ssw_service.controller_create($scope, $rootScope, $firebase, $location, $route, $routeParams.id, fbURL, ssw_service, 'team');
})

.controller('requestControllerCreate', function($scope, $rootScope, $firebase, $location, $route, $routeParams, fbURL, ssw_service) {
    ssw_service.controller_create($scope, $rootScope, $firebase, $location, $route, $routeParams.id, fbURL, ssw_service, 'request');
})

.controller('authController', function($scope, $rootScope, $firebase, $firebaseSimpleLogin, $location, $route, fbURL, ssw_service) {
    ssw_service.update_auth($rootScope, $firebase, $firebaseSimpleLogin, fbURL, ssw_service);
    
    $scope.signIn = function() {
        $rootScope.lObj = $firebaseSimpleLogin(new Firebase(fbURL));
        $rootScope.lObj.$login('password', {
            email: $scope.email,
            password: $scope.password,
            rembemberMe: false
        })
        .then(
            function(luser) {
                ssw_service.update_auth($rootScope, $firebase, $firebaseSimpleLogin, fbURL, ssw_service);
                console.log('Logged in as: ', luser.uid);
                $location.path('/home');
            },
            function(error) {
                console.log('Login failed: ', error);
                alert('Invalid login');
            }
        );
    };
    
    $scope.signOut = function() {
        $rootScope.lObj.$logout();
        ssw_service.update_auth($rootScope, $firebase, $firebaseSimpleLogin, fbURL, ssw_service);
        console.log('Logged out');
        $location.path('/home');
    };
})

/*
.controller('userControllerCreate', function($scope, $rootScope, $firebase, $firebaseSimpleLogin, $location, fbURL, users, userinfos) {

    //var userinfos = $firebase(new Firebase(fbURL + 'userinfos'));
    //var users = $firebase(new Firebase(fbURL + 'users'));
    
    $rootScope.lObj = $firebaseSimpleLogin(new Firebase(fbURL));

    $scope.userDetail = true;
    $scope.auth = $rootScope.auth;
    $scope.user = {};
    $scope.userinfo = {};

    $scope.cancel = function() {
        $location.path('/home');
    };
    
    $scope.signUp = function() {
        if ($scope.password !== $scope.password2) {
            alert('Passwords do not match');
            $location.path('/register');
        }
        else {
            $rootScope.lObj.$createUser($scope.email, $scope.password, false)
            .then(
                function(luser) {
                    $scope.save($firebase, luser);
                    alert('User ' + $scope.email + ' registered successfully');
                    $location.path('/users');
                },
                function(error) {
                    var code = error.code;
                    console.log('Login failed: ', error);
                    if (code === "INVALID_EMAIL") {
                        alert('Invalid email' + $scope.email);
                    }
                    else if (code === "EMAIL_TAKEN") {
                        alert('User ' + $scope.email + ' already registered');
                    }
                    else if (code === "INVALID_PASSWORD") {
                        alert('Invalid password' + $scope.password);
                    }
                    else {
                        alert('Error in registration: ' + code);
                    }
                    $location.path('/register');
                }
            );
        }
    };
    
    $scope.save = function($firebase, luser) {
        var user = {};
        var userinfo = $scope.userinfo;
        userinfos.$add(userinfo)
        .then(
            function(refInfo) {
                userinfo.id = refInfo.name();
                userinfo.permission = 'user';
                refInfo.set(userinfo);
                user = {
                    username: luser.email, 
                    info: refInfo.name(),
                    aid: luser.id,
                    child: "userinfo"
                };
                users.$add(user)
                .then(
                    function(refUser) {
                        user.id = refUser.name();
                        refUser.set(user);
                    },
                    function(error) {
                        console.log(error);
                    }
                );
            },
            function(error) {
                console.log(error);
            }
        );
    };
})
*/
/*
.controller('teamControllerCreate', function($scope, $rootScope, $location, fbURL) {
    //$rootScope.auth = $firebaseAuth(new Firebase(fbURL));
    $scope.teamDetail = true;
    $scope.teamCreate = false;
    $scope.team = {};
    //$rootScope.luser = {};
        
    $rootScope.auth = new FirebaseSimpleLogin(new Firebase(fbURL), function(error, luser) {
        if (luser) {
            console.log('User ID: ' + luser.uid + ', Provider: ' + luser.provider);
            $scope.teamCreate = true;
        }
    });
    
    $scope.cancel = function() {
        $location.path('/teams');
    };
    
    $scope.save = function() {      
        var tRefs = new Firebase(fbURL + 'teams/');
        var tRef  = tRefs.push($scope.team, function(err) {
            var tid = tRef.name();
            $scope.team.id = tid;
            $scope.team.manager = $rootScope.luser.ssw_uid;
            tRef.set($scope.team);
            alert('New field ' + $scope.team.name + ' was successfully registered');
            $location.path('/teams');
        });
    };
    
})
*/

;

